import java.util.ArrayList;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Point;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

public class FuncGrapher extends JFrame {
	
	protected class DrawPanel extends JPanel {
		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			//g.drawString("Hello", 0, 12);
			if (fd2 != null) {
				int halfW = draw.getWidth()/2;
				int halfH = draw.getHeight()/2;
				double zoom = 20;
				//axes
				g.setColor(Color.gray);
				g.drawLine(0, halfH, draw.getWidth(), halfH); //X
				g.drawLine(halfW, 0, halfW, draw.getHeight()); //Y
				//function
				g.setColor(Color.black);
				for (int x=-halfW; x<halfW; x++) {
					g.fillOval(x+halfW, (int)(halfH-zoom*fd2.getPoint(x/zoom)), 3, 3);
				}
				//roots
				double[] roots = fd2.getRoots();
				int rootSz = 10;
				g.setColor(Color.red);
				if (roots != null) {
					for (double r: roots)
						g.drawOval((int)(halfW+r*zoom)-rootSz/2, halfH-rootSz/2, rootSz, rootSz);
				}
			}
		}
	}
	
	
	protected JPanel draw;	
	protected FonctionDegre2 fd2;
	protected final JTextField txtA, txtB, txtC;
	
	public FuncGrapher() {
		setSize(600, 450);
		setLocation(200, 200);
		setTitle("Grapheur du second degré");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		((JComponent)getContentPane()).setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		
		JPanel sup = new ThinPanel();
		sup.setLayout(new BoxLayout(sup, BoxLayout.X_AXIS));
		sup.add(new JLabel("y="));
		txtA = new ThinTextField(4);
		sup.add(txtA);
		sup.add(new JLabel("x²+"));
		txtB = new ThinTextField(4);
		sup.add(txtB);
		sup.add(new JLabel("x+"));
		txtC = new ThinTextField(4);
		sup.add(txtC);
		sup.add(Box.createHorizontalStrut(20));
		JButton btnDraw = new JButton("Draw");
		btnDraw.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					double a, b, c;
					a = Double.parseDouble(txtA.getText());
					b = Double.parseDouble(txtB.getText());
					c = Double.parseDouble(txtC.getText());
					fd2 = new FonctionDegre2(a, b, c);
					draw.repaint();
				}
				catch (NumberFormatException ex) {
					JOptionPane.showMessageDialog(null, "Veuillez remplir tous les champs avec des nombres réels", "Erreur de saisie", JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		sup.add(btnDraw);
		
		//add(new JLabel("Enter parameters for function Y=aX²+bX+c"));
		add(sup);
		add(Box.createVerticalStrut(5));
		add(draw = new DrawPanel());
		draw.setBackground(Color.white);
		
		setVisible(true);
	}
	
	public static void main(String[] args)
	{
		FuncGrapher gr = new FuncGrapher();
	}
}

